package com.zee.wechat.module.sys.controller;

import com.zee.wechat.framework.configuration.shrio.MyUsernamePasswordToken;
import com.zee.wechat.framework.enums.ResultCode;
import com.zee.wechat.framework.vo.ResponseResult;
import com.zee.wechat.module.sys.service.UserInfoService;
import com.zee.wechat.module.sys.dto.LoginDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


@RestController
@RequestMapping("/userInfo")
@Api(tags = "用户信息")
public class UserInfoController {
    private static final Logger logger = LoggerFactory
            .getLogger(UserInfoController.class);
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "用户登录", notes = "用户登录（如果用游客身份登录需传参userType:guest同时账号密码可以不传）")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginDto loginVo) {
        ResponseResult resultVo;
        Subject subject = SecurityUtils.getSubject();
        MyUsernamePasswordToken token = new MyUsernamePasswordToken(loginVo.getUsername(), loginVo.getPassword(), loginVo.getOnDuty());
        try {
            subject.login(token);
            resultVo = new ResponseResult(ResultCode.SUCCESS);
        } catch (IncorrectCredentialsException e) {
            resultVo = new ResponseResult(ResultCode.ERR10002);
        } catch (LockedAccountException e) {
            resultVo = new ResponseResult(ResultCode.ERR10003);
        } catch (AuthenticationException e) {
            resultVo = new ResponseResult(ResultCode.ERR10002);
        }
        return resultVo;
    }

    /**
     * 未登录，shiro应重定向到登录界面
     * 此处返回未登录状态信息由前端控制跳转页面
     */
    @RequestMapping(value = "/unAuth", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiIgnore
    public ResponseResult unAuth() {
        return new ResponseResult(ResultCode.ERR10005);
    }


}
